<template>
  <div class="hello">
    <h1>{{ msg }}</h1>
    <h3>数量{{ count }}</h3>
    <button @click="incress">增加数量{{}}</button>
    <h3>数量{{ num }}</h3>
    <button @click="add">增加数量</button>
    <h4>{{ message }}位置x{{ x }}y{{ y }}</h4>
    <h3>vuex{{ maxCount }}</h3>
    <button @click="handSend">点击传递值</button>
    <slot name="footer"> </slot>
  </div>
</template>

<script lang="ts">
import usePosition from "@/hooks/usePosition";
import {
  computed,
  defineComponent,
  onMounted,
  reactive,
  ref,
  toRefs,
  useAttrs,
  watch,
} from "vue";
import { useStore } from "vuex";

export default defineComponent({
  name: "BaseCode",
  props: {
    msg: String,
  },
  emits: ["send-value"],
  beforeRouteEnter(to: any, from: any, next: any) {
    next();
  },
  setup(props, context) {
    const count = ref(0);
    const store = useStore();
    const maxCount = computed(() => store.getters.getMaxCount);
    const incress = () => {
      count.value++;
    };
    const emit = context.emit;

    const handSend = () => {
      emit("send-value", 10000);
    };

    const attributes = useAttrs();

    const value = reactive({
      num: 0,
    });
    const add = () => {
      value.num = value.num + 1;
    };
    // 监听数据
    watch(
      () => value.num,
      (val) => {
        console.log("打印数量", val);
      }
    );
    const message = computed(() => `展示信息${value.num}`);
    onMounted(() => {
      store.dispatch("fetchMaxCount", { data: 111 });
      console.log("挂载", attributes);
    });

    const { x, y } = usePosition();

    return {
      count,
      incress,
      add,
      ...toRefs(value),
      message,
      x,
      y,
      maxCount,
      handSend,
    };
  },
});
</script>

<!-- Add "scoped" attribute to limit CSS to this component only -->
<style scoped lang="scss">
h3 {
  margin: 40px 0 0;
}

ul {
  list-style-type: none;
  padding: 0;
}

li {
  display: inline-block;
  margin: 0 10px;
}

a {
  color: #42b983;
}
</style>
